import httpFetch from 'share/httpFetch'
import config from 'config'
import errorMessage from "share/errorMessage"

export default {
  /**
   * 获取公务卡消费记录
   * @param {String} start 
   * @param {String} end 
   */
  getList (searchCondition, pagination) {
    let params = {
      page: pagination.page,
      size: pagination.size,
      start: searchCondition.start,
      end: searchCondition.end,
      orderColumn: searchCondition.orderColumn,
      userOid: searchCondition.userOID
    }
    return new Promise((resolve, reject) => {
      httpFetch.get(`${config.baseUrl}/api/bankbill/origin`, params).then((res) => {
        resolve(res);
      }).catch(err => {
        errorMessage(err.response);
        reject(err.response);
      });
    })
  },
  /**
   * 拆分一条数据
   */
  splitRecord (splitArr) {
    return new Promise((resolve, reject) => {
      httpFetch.post(`${config.baseUrl}/api/bankbill`, splitArr).then((res) => {
        resolve(res);
      }).catch(err => {
        errorMessage(err.response);
        reject(err.response);
      });
    })
  },
  /**
   * 删除一条已经拆分的记录
   */
  deleteSplit (ids) {
    return new Promise((resolve, reject) => {
      httpFetch.delete(`${config.baseUrl}/api/bankbill/split`, ids).then((res) => {
        resolve(res);
      }).catch(err => {
        errorMessage(err.response);
        reject(err.response);
      });
    })
  },
  /**
   * 根据id数组获取这些id对应的公务卡消费详情
   */
  getRecordsByIds (ids) {
    return new Promise((resolve, reject) => {
      httpFetch.get(`${config.baseUrl}/api/bankbill/origin/ids?ids=${ids.join(',')}`).then((res) => {
        resolve(res);
      }).catch(err => {
        errorMessage(err.response);
        reject(err.response);
      });
    })
  },
  /**
   * 公务卡消费转因私
   */
  toBorrow (paramsBody, paramsUrl) {
    let url = `${config.baseUrl}/api/bankbill/relation/loanbill`
    const keysArr = Object.keys(paramsUrl)
    if (keysArr.length > 0) {
      keysArr.map((key, index) => {
        if (index === 0) {
          url = `${url}?${key}=${paramsUrl[key]}`
        } else {
          url = `${url}&${key}=${paramsUrl[key]}`
        }
      })
    }
    return new Promise((resolve, reject) => {
      httpFetch.put(encodeURI(url), paramsBody).then((res) => {
        resolve(res);
      }).catch(err => {
        errorMessage(err.response);
        reject(err.response);
      });
    })
  }
}
